/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.report.accounting;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import src.DBManager;
import src.domain.gl.Glaccnt;
import src.domain.gl.Gledger;
import src.domain.system.Branch;
import src.report.AbstractBaseReportBean;
import src.services.CRUEDService;
import src.view.system.CompanyBean;

/**
 *
 * @author Lalugina
 */
@ManagedBean
@SessionScoped
public class RptGledgerBean extends AbstractBaseReportBean{
    private final String COMPILE_FILE_NAME = "gl/gledger";
    private List<Gledger> gledgers;
    private Date startDate, endDate;
    private Integer branchId;
    private Glaccnt glaccnt;

    public RptGledgerBean() {
        
    }

    @Override
    protected JRDataSource getJRDataSource() {
        RptGledgerDS ds = new RptGledgerDS(gledgers);
        return ds;
    }

    @Override
    protected String getCompileFileName() {
        return COMPILE_FILE_NAME;
    }

    public String execute(List<Gledger> reports) {
	try {
            
            String branchName = "Semua Cabang";
            if (branchId>0) branchName = ((Branch) new CRUEDService().getById(Branch.class, branchId)).getBranchName();
            String period = src.util.Utils.formatDate(startDate).concat(" s/d ")
                    .concat(src.util.Utils.formatDate(endDate)).concat(" ").concat(branchName);
            
            super.getReportParameters().put("p_company", new CompanyBean().loadCompanyInfo().toString());
            super.getReportParameters().put("p_period", period);
            super.getReportParameters().put("p_akun", glaccnt.getAccountNo().concat(" ").concat(glaccnt.getAccountName()));
            
            DBManager db = new DBManager();
            db.getConnection();
            super.getReportParameters().put("REPORT_CONNECTION", db.conn);
            this.gledgers = reports;
            super.prepareReportViaConn(getJRDataSource());
            db.putConnection();
	} catch (JRException | IOException e) {}
	
        return null;
    }

    public void setBranchId(Integer branchId) {
        this.branchId = branchId;
    }
    
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public List<Gledger> getGledgers() {
        return gledgers;
    }

    public void setGledgers(List<Gledger> gledgers) {
        this.gledgers = gledgers;
    }

    public Integer getBranchId() {
        return branchId;
    }

    public Date getEndDate() {
        return endDate;
    }

    public Date getStartDate() {
        return startDate;
    }

    public Glaccnt getGlaccnt() {
        return glaccnt;
    }

    public void setGlaccnt(Glaccnt glaccnt) {
        this.glaccnt = glaccnt;
    }
    
    
    
    
    
    
}
